<?php

//仓库
namespace Common\Model;

class UserKuerpWarehouseModel extends BaseModel {

    public $append = [
        'loc_num',      //库位数量
        'product_num',  //商品总数
        'stock_num',    //库存总数量
	];

    /*公用方法*/
    //仓库名称
    public function getWhNameAttr($value, $data){
        $name = '';
		$model = M('UserKuerpWarehouse');
		$wdata['id'] = $data['wh_id'];
		$name = $model->where($wdata)->getField('name') ? : '';
		return $name;
    }

    /**
     * 商品名称
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getProductNameAttr($value, $data){
        $product_name = '';
        if($data['item_id']){
            $model = M('user_goods_attr');
            $wdata['id'] = $data['item_id'];
            $vo = $model->where($wdata)->find();
            $product_name = $vo['goods_name'].'-'.$vo['guige_name'].' '.$vo['color'];
        }else{
            $model = M('user_goods');
            $wdata['id'] = $data['product_id'];
            $vo = $model->where($wdata)->find();
            $product_name = $vo['goods_name'];
        }
        return $product_name;
    }

    //库位名称
    public function getLocNameAttr($value, $data){
        $model = M('UserKuerpWarehouseLocation');
        $wdata['id'] = $data['loc_id'];
        $name = $model->where($wdata)->getField('loc_name');
		return $name;
    }

    //库位数量
    public function getLocNumAttr($value, $data){
        $model = M('UserKuerpWarehouseLocation');
        $wdata['wh_id'] = $data['id'];
        $count = $model->where($wdata)->count();
		return $count;
    }

    /**
     * 库存总数
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getStockNumAttr($value, $data){
        $num = 0;
        $model = M('UserKuerpWarehouseLocProduct');
        $wdata['wh_id'] = $data['id'];
        $num = $model->where($wdata)->sum('stock') ? : 0;
		return floatval($num);
    }

    /**
     * 商品总数
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getProductNumAttr($value, $data){
        $num = 0;
        $model = M('UserKuerpWarehouseProduct');
        $wdata['wh_id'] = $data['id'];
        $num = $model->where($wdata)->distinct('product_id')->count() ? : 0;
		return $num;
    }

    /**
     * 获取商品库位库存(有skuid atype和product_id可不传)
     * @param $input['atype']:商品类型 1=成品，2=主辅料 ，10=其他商品 	
     * @param $input['product_id']:商品id
     * @param $input['item_id']:skuid
     * @param $input['loc_id']:库位id
     * @return mixed
     */
    function getProductLocStock($input){
        $model = M('UserKuerpWarehouseLocProduct');
        $wdata['loc_id'] = $input['loc_id'];
        if(!$input['item_id'] && (!$input['atype'] || !$input['product_id'])){
             return 0;
        }
        if(!$input['item_id']){
            $wdata['atype'] = $input['atype'];
            $wdata['product_id'] = $input['product_id'];
        }
        $wdata['item_id'] = $input['item_id'];
        $stock = $model->where($wdata)->getField('stock');
        //dump(M('')->_sql());
        return floatval($stock) ? : 0;
    }

    //计算库位数量
    function tjkw($wh_id){
        $l_model = M('UserKuerpWarehouseLocation');
        $l_wdata['wh_id'] = $wh_id;
        $l_wdata['is_delete'] = 0;
        $count = $l_model->where($l_wdata)->count();
        $wdata['id'] = $wh_id;
        $sdata['loc_num'] = $count;
        $this->where($wdata)->save($sdata);
    }

    //搜索条件
    public function _after_search(){
        $map = array();
        $model = D(CONTROLLER_NAME);
        $fileds = $model->getDbFields();
        if($fileds['is_del']){
            $map['is_del'] = 0;
        }
        //$map['is_del'] = 0;
        return $map;
    }

    //获取列表
    function lists(){
        $kwh_model = M('UserKuerpWarehouse');
        $kwh_wdata['status'] = 1;
        $kwh_list = $kwh_model->where($kwh_wdata)->order('id desc')->select();
        return $kwh_list;
    }

    //获取商品列表

	 /**
     +----------------------------------------------------------
     * 入库
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $request 提交数据
     * @param array $request['supplier_id'] 供应商id
     * @param array $request['order_id'] 订单id
     * @param array $request['staff_id'] 员工id
     * @param array $request['uid'] 客户id
     * @param array $request['resource_type'] 资源类型 purchase:采购 sale:销售 inv:盘点 produce:生产
     * @param array $request['order_sn'] 订单号
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */
    function into($request){
        $return = [
            'status' => false,
            'errcode'   => 10000,
            'data'   => '',
            'msg'    => '失败'
        ];
        $sqls = array();
        $kpd_model = M('UserKuerpWarehouseLog');
        //$data = $kpd_model->create();
        $data = $request;
        if($request['nums']<1){
            $return['msg'] = '数量不能低于1,必须是正整数 - 1000';
            return $return;
        }
        if(!$request['wh_id']){
            $return['msg'] = '仓库必须 - 1000-2';
            return $return;
        }
        $kwl_model = M('UserKuerpWarehouseLocation');
        $kwl_wdata['uid'] = $request['uid'];
        $kwl_wdata['wh_id'] = $request['wh_id'];
        $kwl_wdata['id'] = $request['loc_id'];
        $kwl_count = $kwl_model->where($kwl_wdata)->count();
        if($kwl_count==0){
            $return['msg'] = '库位不存在 - 1000-3';
            return $return;
        }
        $p_model = D('UserGoods');
        $p_wdata['uid'] = $request['uid'];
        $p_wdata['id'] = $request['product_id'];
        $p_vo = $p_model->where($p_wdata)->find();
        if(!$p_vo){
            $return['msg'] = '不是你的商品不能入库 - 1000-2';
            return $return;
        }
        $product_info = $p_vo['goods_name'].','.$p_vo['goods_sn'].','.$p_vo['goods_attr'].','.$p_vo['chenfen'].','.$p_vo['unit'];
        $pa_model = D('UserGoodsAttr');
        $pa_wdata['goods_id'] = $request['product_id'];
        $pa_wdata['id'] = $request['item_id'];
        $pa_vo = $pa_model->where($pa_wdata)->find();
        //dump(M()->_sql());
        if(!$pa_vo){
            $return['msg'] = '不是你的商品不能入库 - 1000-3';
            return $return;
        }
        if($request['item_id']){
            $total_stock = $pa_model->getStock($request['item_id']);
        }else if($request['product_id']){
            $total_stock = $p_model->getStock($request['product_id']);
        }
        $total_stock += $request['nums'];
        $type = 1;//入库
        $goods_sn = $p_vo['goods_sn'];//货号
        if(!$goods_sn){
            $return['msg'] = '商品`'.$p_vo['title'].'`货号不存在,请先给商品添加货号 - 1000-6-2';
            return $return;
        }
        $relation = array();
        $info = '';
        //$data['resource_type'] = $request['resource_type'];
        $data['msg'] = $info;
        $data['remark'] = $request['remark'] ? : '';
        //仓库商品库存
        $kwp_model = M('UserKuerpWarehouseProduct');
        $kwp_wdata['uid'] = $request['uid'];
        $kwp_wdata['wh_id'] = $request['wh_id'];
        $kwp_wdata['product_id'] = $request['product_id'];
        $kwp_wdata['item_id'] = $request['item_id'] ? : 0;
        $kwp_vo = $kwp_model->where($kwp_wdata)->find();
        if($kwp_vo){
            $kwp_sdata['stock'] = array('exp','stock+'.$request['nums']);
            $kwp_sdata['edit_time'] = NOW_TIME;
            $res = $kwp_model->where($kwp_wdata)->save($kwp_sdata);
            if(!$res){
                $return['msg'] = '仓库商品库存修改失败 - 1002';
                return $return;
            }
            $before_wh_stock = $kwp_vo['stock'];
        }else{
            $kwp_data = $kwp_wdata;
            $kwp_data['goods_sn'] = $goods_sn;
            $kwp_data['stock'] = $request['nums'];
            $kwp_data['edit_time'] = NOW_TIME;
            $res = $kwp_model->add($kwp_data);
            if(!$res){
                $return['msg'] = '仓库商品库存添加失败 - 1003';
                return $return;
            }
            $before_wh_stock = 0;
        }
        $now_wh_stock = $before_wh_stock+$request['nums'];
        $sqls[] = M()->_sql();
        //仓库之前库存
        $data['before_wh_stock'] = $before_wh_stock;
        $data['now_wh_stock'] = $now_wh_stock;
        //仓库库位商品库存
        $kwlp_model = M('UserKuerpWarehouseLocProduct');
        $kwlp_wdata['uid'] = $request['uid'];
        $kwlp_wdata['wh_id'] = $request['wh_id'];
        $kwlp_wdata['loc_id'] = $request['loc_id'];
        $kwlp_wdata['product_id'] = $request['product_id'];
        $kwlp_wdata['item_id'] = $request['item_id'] ? : 0;
        $kwlp_vo = $kwlp_model->where($kwlp_wdata)->find();
        if($kwlp_vo){
            $kwlp_sdata['stock'] = array('exp','stock+'.$request['nums']);
            $kwlp_sdata['edit_time'] = NOW_TIME;
            $res = $kwlp_model->where($kwlp_wdata)->save($kwlp_sdata);
            if(!$res){
                $return['msg'] = '库位商品库存修改失败 - 1004';
                return $return;
            }
            $before_loc_stock = $kwlp_vo['stock'];
        }else{
            $kwlp_data = $kwlp_wdata;
            $kwlp_data['goods_sn'] = $goods_sn;
            $kwlp_data['stock'] = $request['nums'];
            $kwlp_data['edit_time'] = NOW_TIME;
            $res = $kwlp_model->add($kwlp_data);
            if(!$res){
                $return['msg'] = '库位商品库存添加失败 - 1005';
                return $return;
            }
            $before_loc_stock = 0;
        }
        $now_loc_stock = $before_loc_stock+$request['nums'];
        $sqls[] = M()->_sql();

        //库位之前库存
        $data['before_loc_stock'] = $before_loc_stock;
        $data['now_loc_stock'] = $now_loc_stock;
        $data['total_stock'] = $total_stock;//总数量
        //增加历史数据
        $data['uid'] = $request['uid'] ? : 0;
        $data['change_id'] = $request['change_id'] ? : 0;
        $data['order_id'] = $request['order_id'];
        $data['goods_sn'] = $goods_sn ? : '';
        $data['product_info'] = $product_info ? : '';
        $data['type'] = 1;//入库
        $data['add_time'] = NOW_TIME;
        $res = $kpd_model->add($data);
        if(!$res){
            $return['msg'] = '历史记录添加出错 - 1005';
            return $return;
        }
        $sqls[] = M()->_sql();
        //dump($sqls);exit;
        //$return['data'] = $return_data;
        $return['sqls'] = $sqls;
        $return['data'] = $res;
        $return['status'] = true;
        $return['msg'] = '入库成功';
        return $return;
    }


    //出库
    function out($request){
        $return = [
            'status' => false,
            'errcode'   => 10000,
            'data'   => '',
            'msg'    => '失败'
        ];
        $sqls = array();
        $kpd_model = M('UserKuerpWarehouseLog');
        //$data = $kpd_model->create();
        $data = $request;
        if($request['nums']<1){
            $return['msg'] = '数量不能低于1,必须是正整数 - 1000';
            return $return;
        }
        if(!$request['wh_id']){
            $return['msg'] = '仓库必须 - 1000';
            return $return;
        }
        if(!$request['loc_id']){

        }else{
            $kwl_model = M('UserKuerpWarehouseLocation');
            $kwl_wdata['uid'] = $request['uid'];
            $kwl_wdata['wh_id'] = $request['wh_id'];
            $kwl_wdata['id'] = $request['loc_id'];
            $kwl_count = $kwl_model->where($kwl_wdata)->count();
            if($kwl_count==0){
                echo M()->_sql();exit;
                $return['msg'] = '你所在的库位不在此仓库 - 1000-3';
                return $return;
            }
        }

        if($request['item_id']){
            //有规格商品
            $p_model = D('UserGoodsAttr');
            $p_wdata['id'] = $request['item_id'];
            $p_vo = $p_model->where($p_wdata)->find();
        }else{
            //商品
            $p_model = D('UserGoods');
            $p_wdata['id'] = $request['product_id'];
            $p_vo = $p_model->where($p_wdata)->find();
        }
        if($request['item_id']){
            $total_stock = $p_model->getStock($request['item_id']);
        }else if($request['product_id']){
            $total_stock = $p_model->getStock($request['product_id']);
        }
        $total_stock -= $request['nums'];
        if(!$p_vo){
            $return['msg'] = '商品不存在 - 1003';
            return $return;
        }
        $product_info = $p_vo['goods_name'].','.$p_vo['goods_sn'].','.$p_vo['goods_attr'].','.$p_vo['chenfen'].','.$p_vo['unit'];
        $relation = array();
        $info = '';
        $wl_model = M('UserKuerpWarehouseLocProduct');
        $wl_wdata['uid'] = $request['uid'];
        $wl_wdata['wh_id'] = $request['wh_id'];
        $wl_wdata['loc_id'] = $request['loc_id'];
        $wl_wdata['product_id'] = $request['product_id'];
        $wl_wdata['item_id'] = $request['item_id'];
        $wl_vo = $wl_model->where($wl_wdata)->find();
        if(!$wl_vo){
            $return['msg'] = '库存信息不存在 - 1003';
            return $return;
        }
        /*
        if($wl_vo['stock']<$request['nums']){
            $return['msg'] = '出库数量不能大于库存-['.$p_vo['goods_name'].'] - 1003';
            return $return;
        }
        */
        $data['remark'] = $request['remark'] ? : '';
        //仓库商品库存
        $kwp_model = M('UserKuerpWarehouseProduct');
        $kwp_wdata['uid'] = $request['uid'];
        $kwp_wdata['wh_id'] = $request['wh_id'];
        $kwp_wdata['product_id'] = $request['product_id'];
        $kwp_wdata['item_id'] = $request['item_id'] ? : 0;
        $kwp_vo = $kwp_model->where($kwp_wdata)->find();
        /*
        if($kwp_vo['stock']<$request['nums']){
            $return['msg'] = '出库数量不能大于库存 - 1006 -'.$p_vo['id'];
            return $return;
        }
        */
        if($kwp_vo){
            $kwp_sdata['stock'] = array('exp','stock-'.$request['nums']);
            $kwp_sdata['edit_time'] = NOW_TIME;
            $res = $kwp_model->where($kwp_wdata)->save($kwp_sdata);
            if(!$res){
                $return['msg'] = '仓库商品库存修改失败 - 1007';
                return $return;
            }
            $before_wh_stock = $kwp_vo['stock'];
        }else{
            $return['msg'] = '仓库无此商品 - 1008';
            return $return;
        }
        $now_wh_stock = $before_wh_stock-$request['nums'];
        $sqls[] = M()->_sql();
        //仓库之前库存
        $data['before_wh_stock'] = $before_wh_stock;
        $data['now_wh_stock'] = $now_wh_stock;
        //仓库库位商品库存
        $kwlp_model = M('UserKuerpWarehouseLocProduct');
        $kwlp_wdata['uid'] = $request['uid'];
        $kwlp_wdata['wh_id'] = $request['wh_id'];
        $kwlp_wdata['loc_id'] = $request['loc_id'];
        $kwlp_wdata['product_id'] = $request['product_id'];
        $kwlp_wdata['item_id'] = $request['item_id'] ? : 0;
        $kwlp_vo = $kwlp_model->where($kwlp_wdata)->find();
        if($kwlp_vo){
            $kwlp_sdata['stock'] = array('exp','stock-'.$request['nums']);
            $kwlp_sdata['edit_time'] = NOW_TIME;
            $res = $kwlp_model->where($kwlp_wdata)->save($kwlp_sdata);
            if(!$res){
                $return['msg'] = '库位商品库存修改失败 - 1009';
                return $return;
            }
            $before_loc_stock = $kwlp_vo['stock'];
        }else{
            $return['msg'] = '库位无此商品 - 1010';
            return $return;
        }
        $now_loc_stock = $before_loc_stock-$request['nums'];
        $sqls[] = M()->_sql();

        //库位之前库存
        $data['before_loc_stock'] = $before_loc_stock;
        $data['now_loc_stock'] = $now_loc_stock;
        $data['total_stock'] = $total_stock;//总数量
        //增加历史数据
        $data['uid'] = $request['uid'];
        $data['change_id'] = $request['change_id'] ? : 0;
        $data['goods_sn'] = $goods_sn ? : '';
        $data['type'] = 2;
        $data['add_time'] = NOW_TIME;
        $data['order_id'] = $request['order_id'];
        $data['wuliu_no'] = $request['wuliu_no'] ? : '';
        $data['product_info'] = $product_info ? : '';
        $res = $kpd_model->add($data);
        if(!$res){
            $return['msg'] = '历史记录添加出错 - 1011';
            return $return;
        }
        $sqls[] = M()->_sql();
        //dump($sqls);exit;
        //$return['data'] = $return_data;
        $return['sqls'] = $sqls;
        $return['data'] = $res;
        $return['status'] = true;
        $return['msg'] = '出库成功';
        return $return;
    }

    //普通调拨 
    function transfer($input){
        $return = [
            'status' => false,
            'errcode'   => 10000,
            'data'   => '',
            'msg'    => '失败'
        ];
        $kwlp_model = D('UserKuerpWarehouseLocProduct');
        $kwlp_wdata['id'] = $input['kwlp_id'];
        $kwlp_vo = $kwlp_model->where($kwlp_wdata)->find();
        if(!$kwlp_vo){
            $return['msg'] = '出错 - 1000';
            return $return;
        }
        $sqls = array();
        //出库
        $out_request = $input;
        $out_request['wh_id'] = $kwlp_vo['wh_id'];
        $out_request['loc_id'] = $kwlp_vo['loc_id'];
        $out_request['atype'] = $kwlp_vo['atype'];
        $out_request['product_id'] = $kwlp_vo['product_id'];
        $out_request['item_id'] = $kwlp_vo['item_id'];
        $out_request['nums'] = $input['nums'];
        $out_request['resource_type'] = $input['resource_type'];
        $res = $this->out($out_request);
        if(!$res['status']){
            $return['msg'] = '出库-'.$res['msg'];
            return $return;
        }
        $sqls = array_merge($sqls,$res['sqls']);
        //dump($kwlp_vo);
        //入库
        $input['atype'] = $kwlp_vo['atype'];
        $input['product_id'] = $kwlp_vo['product_id'];
        $input['item_id'] = $kwlp_vo['item_id'];
        $input['remark'] = '订单调拨入库';
        $res = $this->into($input);
        //dump($res);
        if(!$res['status']){
            //$return['msg'] = '订单调拨入库失败 - 1003';
            $return['msg'] = '入库-'.$res['msg'];
            return $return;
        }
        $sqls = array_merge($sqls,$res['sqls']);
        //dump($sqls);exit;
        $return['sqls'] = $sqls;
        $return['status'] = true;
        $return['msg'] = '调拨成功';
        return $return;
    }


    //搜索
    function _before_search(){
        $map = [];
        if(I('keyword')){
            $map['name'] = array('like','%'.I('keyword').'%');
        }
        if(CONTROLLER_NAME=='UserKuerpWarehouse'){
            $map['is_delete'] = 0;
        }
        return $map;
    }


   
}
